package cn.cmcc.online.smsapi.ui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.cmcc.online.smsapi.ui.presenter.CommonPresenter;
import cn.cmcc.online.smsapi.ui.presenter.CommonPresenterImpl;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.widget.CompletedWarpTextView;
import cn.cmcc.online.smsapi.ui.widget.ExpandButton;
import cn.cmcc.online.smsapi.ui.widget.SpotDivier;

/**
 * Created by ZhangYifan on 2017/6/16.
 */

public class CommonVistaImpl extends GeneralBaseVistaImpl implements CommonVista {
    private CommonPresenter mPresenter;

    private LinearLayout mHighLight;
    private TableLayout mList;

    private TextView mHighLightTitle;
    private CompletedWarpTextView mHighLightContent;
    private TextView mNoticeInfo;

    private ExpandButton mExpandButton;
    private View mDivier;

    public CommonVistaImpl(Context context) {
        super(context);
    }

    @Override
    protected void init() {
        super.init();
        mPresenter = new CommonPresenterImpl();
        mPresenter.setVista(this);
    }

    @Override
    protected void initLayout() {
        super.initLayout();

        LinearLayout body = new LinearLayout(mContext);
        body.setOrientation(LinearLayout.VERTICAL);
        body.setBackgroundColor(Color.WHITE);
        body.setPadding(0, 0, 0, DensityUtil.dp2px(mContext, 15));
        body.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dp2px(mContext, 300), -2));
        //----------------------------------------------------------------
        mHighLight = new LinearLayout(mContext);
        mHighLight.setOrientation(LinearLayout.VERTICAL);
        mHighLight.setGravity(Gravity.CENTER_HORIZONTAL);
        mHighLight.setPadding(0, 0, 0, DensityUtil.dp2px(mContext, 15));

        mHighLightTitle = new TextView(mContext);
        mHighLightTitle.setTextColor(Color.parseColor("#999999"));
        mHighLightTitle.setTextSize(12);
        LinearLayout.LayoutParams highLightTitleParams = new LinearLayout.LayoutParams(-2, -2);
        highLightTitleParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 30), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 10));
        mHighLight.addView(mHighLightTitle, highLightTitleParams);

        mHighLightContent = new CompletedWarpTextView(mContext);
        mHighLightContent.setTextColor(Color.parseColor("#04BEA4"));
        mHighLightContent.setTextSize(34);
        mHighLightContent.setTextAlignCenter();
        LinearLayout.LayoutParams highLightContentParams = new LinearLayout.LayoutParams(-2, -2);
        highLightContentParams.setMargins(DensityUtil.dp2px(mContext, 15), 0, DensityUtil.dp2px(mContext, 15), 0);
        mHighLight.addView(mHighLightContent, highLightContentParams);

        body.addView(mHighLight, -1, -2);
        //----------------------------------------------------------------
        mList = new TableLayout(mContext);
        mList.setStretchAllColumns(true);

        LinearLayout.LayoutParams midParams = new LinearLayout.LayoutParams(-1, -2);
        midParams.setMargins(0, DensityUtil.dp2px(mContext, 5), 0, 0);
        body.addView(mList, midParams);
        //----------------------------------------------------------------
        mExpandButton = new ExpandButton(mContext);
        mExpandButton.setWithAnimation(WITH_ANIMATION);
        mExpandButton.setStateChangedListener(mPresenter);
        LinearLayout.LayoutParams expandButtonParams = new LinearLayout.LayoutParams(-1, -2);
        expandButtonParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 7), 0);
        body.addView(mExpandButton, expandButtonParams);
        //----------------------------------------------------------------
        mDivier = new SpotDivier(mContext);
        LinearLayout.LayoutParams divierParams = new LinearLayout.LayoutParams(-1, DensityUtil.dp2px(mContext, 2));
        divierParams.setMargins(0, DensityUtil.dp2px(mContext, 15), 0, 0);
        body.addView(mDivier, divierParams);
        //----------------------------------------------------------------
        mNoticeInfo = new TextView(mContext);
        mNoticeInfo.setTextSize(12);
        mNoticeInfo.setTextColor(Color.parseColor("#999999"));
        LinearLayout.LayoutParams lessImportContentParams = new LinearLayout.LayoutParams(-1, -2);
        lessImportContentParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 10), DensityUtil.dp2px(mContext, 15), 0);
        body.addView(mNoticeInfo, lessImportContentParams);

        super.addBody(body);
    }

    @Override
    public void bind(Context context, JSONObject data) {
        super.bind(context, data);
        mPresenter.bind(data);
    }

    @Override
    public void initDefaultView() {
        mList.removeAllViews();
        super.initDefaultView();
    }

    @Override
    public void setHighInfo(String highlightTitle, String highlightContent) {
        mHighLightTitle.setText(highlightTitle);
        mHighLightContent.setText(highlightContent);
    }

    @Override
    public void setHighInfo(int color) {
        mHighLightContent.setTextColor(color);
    }

    @Override
    public void setListInfo(List<TableRow> tableRowList) {
        for (TableRow tableRow : tableRowList) {
            mList.addView(tableRow, -2, -2);
        }
    }

    @Override
    public void setTipInfo(String tipInfo) {
        if (tipInfo != null && tipInfo.length() > 0) {
            mNoticeInfo.setText(tipInfo);
        }
    }

    @Override
    public void setHighInfoVisible(int visible) {
        mHighLight.setVisibility(visible);
    }

    @Override
    public void setListInfoVisible(int visible) {
        mList.setVisibility(visible);
    }

    @Override
    public void setTipInfoVisible(int visible) {
        mNoticeInfo.setVisibility(visible);
        mDivier.setVisibility(visible);

    }

    @Override
    public TableRow createTableRow() {
        final TableRow tableRow = new TableRow(mContext);
        tableRow.setPadding(0, DensityUtil.dp2px(mContext, 10), 0, 0);
        TextView tvTitle = new TextView(mContext);
        tvTitle.setTag("title");
        tvTitle.setGravity(Gravity.LEFT);
        tvTitle.setTextColor(Color.parseColor("#999999"));
        tvTitle.setTextSize(12);
        tvTitle.setPadding(DensityUtil.dp2px(mContext, 15), 0, 0, 0);
        tvTitle.setMaxLines(4);
        tvTitle.setEllipsize(TextUtils.TruncateAt.END);
        TextView tvContent = new TextView(mContext);
        tvContent.setTag("content");
        tvContent.setGravity(Gravity.RIGHT);
        tvContent.setTextColor(Color.parseColor("#333333"));
        tvContent.setTextSize(12);
        tvContent.setPadding(0, 0, DensityUtil.dp2px(mContext, 15), 0);
        tvContent.setMaxLines(4);
        tvContent.setEllipsize(TextUtils.TruncateAt.END);

        tableRow.addView(tvTitle, -1, -2);
        tableRow.addView(tvContent, -1, -2);
        return tableRow;
    }

    @Override
    public TableRow updateTableRow(TableRow tableRow, String title, String content) {
        TextView tvTitle = (TextView) tableRow.findViewWithTag("title");
        TextView tvContent = (TextView) tableRow.findViewWithTag("content");
        tvTitle.setText(title);
        tvContent.setText(content);
        return tableRow;
    }

    @Override
    public List<TableRow> recycleAllTableRows() {
        List<TableRow> tableRows = new ArrayList<>();
        while (mList.getChildCount() > 0) {
            TableRow tableRow = (TableRow) mList.getChildAt(0);
            tableRows.add(tableRow);
            mList.removeView(tableRow);
        }
        return tableRows;
    }

    @Override
    public void setExpandButtonColor(int color) {
        mExpandButton.setColor(color);
    }

    @Override
    public void setExpandButtonVisible(int visible) {
        mExpandButton.setVisibility(visible);
    }

    @Override
    public void setTableLayoutHeight(int height, boolean withAnimation) {
        if (withAnimation) {
            ValueAnimator animator = ValueAnimator.ofInt(mList.getHeight(), height).setDuration(400);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mList.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    mList.setLayoutParams(params);
                }
            });
            animator.start();
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mList.getLayoutParams();
            params.height = height;
            mList.setLayoutParams(params);
        }
    }
}
